﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
using System.IO;

namespace WebBHXH.Entities
{
    class NguoiSudungModel
    {
        public NguoiSudungModel()
        {
        }
        public NguoiSudungModel(string json)
        {
            JObject jObject = JObject.Parse(json);
            maNSD = (string)jObject["Ma_NSD"];
            tenNSD = (string)jObject["Ten_NSD"];
            matKhau = (string)jObject["MatKhau"];
            email = (string)jObject["Email"];
            maTinh = (string)jObject["Ma_Tinh"];
            maHuyen = (string)jObject["Ma_Huyen"];
            maDonViBH = (string)jObject["Ma_DonViBH"];
            loaiNSD = (string)jObject["Loai_NSD"];
            tinhTrang = (int)jObject["TinhTrang"];
            ghiChu = (string)jObject["GhiChu"];
            //ngayTaoMoi = (DateTime)jObject["NgayTaoMoi"];
            //ngayCapNhat = (DateTime)jObject["NgayCapNhat"];
            ngayTaoMoi = new DateTime();
            ngayCapNhat = new DateTime();
        }

        public List<NguoiSudungModel> GetNguoiSudungModel()
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create("" + Util.getUrl() + "Home/GetNguoiSudungModel");

            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            //get response-stream, and use a streamReader to read the content
            List<NguoiSudungModel> listNguoiSudungModel = new List<NguoiSudungModel>();
            using (Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    JavaScriptSerializer jsonserializer = new JavaScriptSerializer();
                    listNguoiSudungModel = jsonserializer.Deserialize<List<NguoiSudungModel>>(sr.ReadToEnd());
                    return listNguoiSudungModel;
                }
            }
            return new List<NguoiSudungModel>();
        }

        public NguoiSudungModel Authentication(string email, string pass)
        {
            System.Net.WebRequest request = System.Net.WebRequest.Create("" + Util.getUrl() + "Home/Authentication?email=" + email + "&pass=" + pass);
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json; charset=utf-8";
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            //get response-stream, and use a streamReader to read the content
            NguoiSudungModel objNguoiSudungModel = new NguoiSudungModel();
            using (Stream s = request.GetResponse().GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    try
                    {
                        JavaScriptSerializer jsonserializer = new JavaScriptSerializer();
                        objNguoiSudungModel = jsonserializer.Deserialize<NguoiSudungModel>(sr.ReadToEnd());
                        return objNguoiSudungModel;
                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }
        }

        private string maNSD;

        public string MaNSD
        {
            get { return maNSD; }
            set { maNSD = value; }
        }
        private string tenNSD;

        public string TenNSD
        {
            get { return tenNSD; }
            set { tenNSD = value; }
        }
        private string matKhau;

        public string MatKhau
        {
            get { return matKhau; }
            set { matKhau = value; }
        }
        private string email;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        private string maTinh;

        public string MaTinh
        {
            get { return maTinh; }
            set { maTinh = value; }
        }
        private string maHuyen;

        public string MaHuyen
        {
            get { return maHuyen; }
            set { maHuyen = value; }
        }
        private string maDonViBH;

        public string MaDonViBH
        {
            get { return maDonViBH; }
            set { maDonViBH = value; }
        }
        private string loaiNSD;

        public string LoaiNSD
        {
            get { return loaiNSD; }
            set { loaiNSD = value; }
        }
        private int tinhTrang;

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }
        private string ghiChu;

        public string GhiChu
        {
            get { return ghiChu; }
            set { ghiChu = value; }
        }
        private DateTime ngayTaoMoi;

        public DateTime NgayTaoMoi
        {
            get { return ngayTaoMoi; }
            set { ngayTaoMoi = value; }
        }
        private DateTime ngayCapNhat;

        public DateTime NgayCapNhat
        {
            get { return ngayCapNhat; }
            set { ngayCapNhat = value; }
        }

    }
}
